package com.geoboard.Fragments;

import com.geoboard.FragInterCommunication;
import com.geoboard.FragInterCommunication.State;
import com.locoboard.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class TabControlFragment extends Fragment
		implements OnTouchListener{
	
	/**** Members *************************************************/
	private TextView postsTab = null;
	private TextView boardTab = null;
	private FragInterCommunication fragComunicator = null;
	
	private State curState = State.POSTS_MAP;
	
	
	/**** Constructors ********************************************/
	public TabControlFragment(){
		
	}
	
	/**** Methods ************************************************/
	public void setSelectedTab(State selectedState){
		if(curState != selectedState){
			switch(selectedState){
				case BOARD_MAP:								
					boardTab.setBackground(getResources().getDrawable(R.drawable.tab_selected));
					postsTab.setBackground(getResources().getDrawable(R.drawable.tab_unselected));		
					curState = State.BOARD_MAP;
					break;
				case POSTS_MAP:
				default:
					postsTab.setBackground(getResources().getDrawable(R.drawable.tab_selected));
					boardTab.setBackground(getResources().getDrawable(R.drawable.tab_unselected));
					curState = State.POSTS_MAP;
			}
		}
	}
	
	/**** Android Callback Methods ********************************/
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		return inflater.inflate(R.layout.frag_tab_control, container, false);
		//return null;
	}
	
	@Override
	public void onAttach(Activity activity){
		super.onAttach(activity);
	}
	
	@Override
	public void onActivityCreated (Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		fragComunicator = (FragInterCommunication)getActivity();		
	}
	
	@Override
	public void onViewStateRestored (Bundle savedInstanceState){
		super.onViewStateRestored(savedInstanceState);
	}
	
	@Override
	public void onStart(){
		super.onStart();
		
		postsTab = (TextView)getActivity().findViewById(R.id.post_tab);
    	postsTab.setOnTouchListener(this);
    	
    	boardTab = (TextView)getActivity().findViewById(R.id.board_tab);
    	boardTab.setOnTouchListener(this);
	}
	
	@Override
	public void onResume(){
		super.onResume();
	}
	
	@Override
	public void onPause(){
		super.onPause();
	}
	
	@Override
	public void onStop(){
		super.onStop();
	}
	
	@Override
	public void onDestroyView(){
		super.onDestroyView();
	}
	
	@Override
	public void onDestroy(){
		super.onDestroy();
	}
	
	@Override 
	public void onDetach(){
		super.onDetach();
	}

	
	/**** OnTouchListener methods ************************************/
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		State newState = null;
		
		if(v.equals(postsTab) ){
			setSelectedTab(State.POSTS_MAP);			
			newState = State.POSTS_MAP;			
			
		} else if(v.equals(boardTab) && curState != State.BOARD_MAP){
			setSelectedTab(State.BOARD_MAP);			
			newState = State.BOARD_MAP;
		}
		if(newState != null && fragComunicator != null){
			fragComunicator.changeState(newState);
		}		
		
		return false;
	}
		
		
		

}
